home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 11.1 KB | 378 lines | [TEXT/MPS ] |
- // UUndo.h
- // Copyright © 1995-96 by Apple Computer, Inc. All rights reserved.
-
- #ifndef __UUNDO__
- #define __UUNDO__
-
- // MacApp
-
- #ifndef __PASCALSTRING__
- #include "PascalString.h"
- #endif
-
- #ifndef __UCOMMAND__
- #include "UCommand.h"
- #endif
-
- #ifndef __ULIST__
- #include "UList.h"
- #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #ifndef __UOBJECT__
- #include "UObject.h"
- #endif
-
- //----------------------------------------------------------------------------------------
- // Forward and external class declarations.
- //----------------------------------------------------------------------------------------
-
- class TAppleEvent;
-
- //------------------------------------------------------------------------------------
- // Constants
- //------------------------------------------------------------------------------------
-
- enum EActionType {kSingleAction, kBeginAction, kEndAction};
-
- enum ERespectMarksChoices {kDontRespectMarks, kRespectMarks};
-
- //----------------------------------------------------------------------------------------
- // CLASS TUndoAction
- //----------------------------------------------------------------------------------------
-
- class TUndoAction : public TObject
- {
- MA_DECLARE_CLASS;
-
- public:
-
- static TUndoAction* NewUndoAction(TCommand* command, EActionType actionType, CommandNumber id);
- // Make an instance of this class.
-
- //------------------------------------------------------------------------------------
- // Variables
- //------------------------------------------------------------------------------------
-
- TCommand* fCommand;
- CommandNumber fID;
- EActionType fActionType;
- Boolean fMark;
-
- //------------------------------------------------------------------------------------
- // Initializer, I<Method> and Free.
- //------------------------------------------------------------------------------------
-
- TUndoAction();
-
- virtual ~TUndoAction();
-
- void IUndoAction(TCommand* command, EActionType actionType, CommandNumber id);
-
- //------------------------------------------------------------------------------------
- // Methods
- //------------------------------------------------------------------------------------
-
- Boolean IsDone();
-
- };
-
- //----------------------------------------------------------------------------------------
- // CLASS TActionStack
- //----------------------------------------------------------------------------------------
-
- class TActionStack : public TList
- {
- MA_DECLARE_CLASS;
-
- public:
-
- static TActionStack* NewActionStack();
- // Make an instance of this class.
-
- //------------------------------------------------------------------------------------
- // Initializer, I<Method> and Free.
- //------------------------------------------------------------------------------------
-
- TActionStack();
-
- virtual ~TActionStack();
-
- void IActionStack();
-
- //------------------------------------------------------------------------------------
- // Methods
- //------------------------------------------------------------------------------------
-
- inline TUndoAction* Top() { return (TUndoAction*) Last(); }
-
- inline TUndoAction* PopAction() { return (TUndoAction*) Pop(); }
-
- void PushAction(TUndoAction* action);
-
- void ClearActionsToMark();
- // Delete and free actions until a marked one is found, or the stack is empty.
-
- TUndoAction* FindTransaction(TCommand* command, Boolean itsForward);
- // Find the action or transaction containing a command.
- // Use kIterateForward for the undo stack, kIterateBackward for redo.
-
- Size PurgeFirstPhase(Size needed) { return Purge(needed, 1); }
- // Unload as much of the stack as needed up to, but not including,
- // the topmost entry in the stack.
-
- Size Purge(Size needed, ArrayIndex numToLeave = 0);
- // Unload up to top.
-
- private:
- // We need to better tune the following number. This number indicates what we would like
- // to be the maximum number of stacked undoable actions at any one time. If your commands
- // retain large amounts of data for performing undo/redo then you may want to reduce this
- // limit. If they are smaller then perhaps the number could be increased. We should also
- // consider having a maximum memory usage setpoint as well but the overhead of keeping
- // a running total is too costly right now.
-
- static const ArrayIndex sMaxDesiredItems; // Maximum number of entried in stack.
- };
-
- //----------------------------------------------------------------------------------------
- // CLASS TUndoHandler
- //----------------------------------------------------------------------------------------
-
- class TUndoHandler : public TObject
- {
- MA_DECLARE_CLASS;
-
- public:
-
- static TUndoHandler* fgUndoHandler; // The undo handler object.
-
- static void InitUUndo();
- // Initialize this unit.
-
- static TUndoHandler* NewUndoHandler();
- // Make an instance of this class.
-
- //------------------------------------------------------------------------------------
- // Variables
- //------------------------------------------------------------------------------------
-
- protected:
-
- TActionStack* fUndoStack; // Stack of commands that could be undone.
- TActionStack* fRedoStack; // Stack of commands that could be redone.
- long fInTransaction; // Keep track of transaction level in case
- // we need to abort.
-
- //------------------------------------------------------------------------------------
- // Initializer, I<Method> and Free.
- //------------------------------------------------------------------------------------
-
- public:
-
- TUndoHandler();
-
- virtual ~TUndoHandler();
-
- void IUndoHandler();
-
- //------------------------------------------------------------------------------------
- // Action History Manipulation
- //------------------------------------------------------------------------------------
-
- void AddActionToHistory(TCommand* command, EActionType actionType, CommandNumber id);
- // Add an action to the list of commands that could be undone.
- // Also clears the redo history.
-
- void BeginAction();
- // Add an begin action to the list of commands that could be undone.
- // Also clears the redo history.
-
- void EndAction();
- // Add an end action to the list of commands that could be undone.
- // Also clears the redo history.
-
- void AbortCurrentTransaction();
- // Remove the current transaction (and any nested transactions) from
- // the action history. Must be called before the End action is added.
-
- void ClearActionHistory(ERespectMarksChoices respectMarks);
- // Clear the undo and redo stacks.
-
- void MarkActionHistory();
- // Marks the top of the undo and redo stacks.
-
- TUndoAction* FindTransaction(TCommand* command);
- // Find the action or transaction containing a particular command.
-
- void Abort();
- // Abort the top (trans)action in the undo stack.
-
- Size Purge(Size needed);
- // Unload as much of the undo and redo stack as needed.
-
- //------------------------------------------------------------------------------------
- // Undo
- //------------------------------------------------------------------------------------
-
- void Undo();
- // Undo the top (trans)action in the undo stack.
-
- Boolean AnythingToUndo();
- // Whether there is anything to undo on the stack.
-
- CommandNumber GetUndoID();
- // Get the ID for the undo menu item for a command.
-
- void GetUndoText(CStr255& undoName);
- // Get the text for the undo menu item for a command.
-
- //------------------------------------------------------------------------------------
- // Redo
- //------------------------------------------------------------------------------------
-
- void Redo();
- // Redo the top (trans)action in the redo stack.
-
- void ClearRedoHistory();
- // Clear the redo stack.
-
- Boolean AnythingToRedo();
- // Whether there is anything to redo on the stack.
-
- CommandNumber GetRedoID();
- // Get the ID for the redo menu item for a command.
-
- void GetRedoText(CStr255& undoName);
- // Get the text for the redo menu item for a command.
-
- //------------------------------------------------------------------------------------
- // For internal use only
- //------------------------------------------------------------------------------------
-
- protected:
-
- EActionType AbortAction();
- // Abort the top action in the undo stack.
-
- void AbortCommand(TCommand* command);
- // Abort a command.
-
- EActionType UndoAction();
- // Undo the top action in the undo stack.
-
- void UndoCommand(TCommand* command);
- // Undo a command.
-
- EActionType RedoAction();
- // Redo the top action in the undo stack.
-
- void RedoCommand(TCommand* command);
- // Redo a command.
- };
-
- //----------------------------------------------------------------------------------------
- // CLASS TUndoRedoCommand
- //----------------------------------------------------------------------------------------
-
- class TUndoRedoCommand : public TCommand
- {
- MA_DECLARE_CLASS;
-
- protected:
-
- AEEventID fAEEventID;
-
- public:
-
- TUndoRedoCommand();
- // Constructor
- virtual ~TUndoRedoCommand();
- // Destructor
-
- void IUndoRedoCommand(CommandNumber itsCommandNumber);
- // Initialize the UndoRedoCommand procedurally.
-
- virtual TAppleEvent* MakeAppleEvent();
- // Create a Undo or Redo Apple Event.
- };
-
- //----------------------------------------------------------------------------------------
- // CLASS TUndoCommand
- //----------------------------------------------------------------------------------------
-
- class TUndoCommand : public TUndoRedoCommand
- {
- MA_DECLARE_CLASS;
-
- public:
-
- TUndoCommand();
- // Constructor
- virtual ~TUndoCommand();
- // Destructor
-
- void IUndoCommand(CommandNumber itsCommandNumber);
- // Initialize the UndoCommand procedurally.
-
- virtual void DoIt();
- // tell the application to undo/redo.
- };
-
- //----------------------------------------------------------------------------------------
- // CLASS TRedoCommand
- //----------------------------------------------------------------------------------------
-
- class TRedoCommand : public TUndoRedoCommand
- {
- MA_DECLARE_CLASS;
-
- public:
-
- TRedoCommand();
- // Constructor
- virtual ~TRedoCommand();
- // Destructor
-
- void IRedoCommand(CommandNumber itsCommandNumber);
- // Initialize the RedoCommand procedurally.
-
- virtual void DoIt();
- // tell the application to undo/redo.
- };
-
- //----------------------------------------------------------------------------------------
- // CLASS CUndoStackHook
- //----------------------------------------------------------------------------------------
-
- // A class for unloading portions of the undo stack when memory is needed.
-
- class CUndoStackHook : public CGrowZoneHook
- {
- public:
- CUndoStackHook() { }
- virtual ~CUndoStackHook() { }
-
- virtual Size TotalSize(Boolean justLocked);
- // Returns the total number of bytes managed (or only locked
- // handles if justLocked is true).
-
- virtual Size Purge(Size needed);
- // Make memory available by unloading the undo stack. Returns
- // the actual amount of memory purged.
- };
-
-
- //========================================================================================
- // GLOBAL Procedures
- //========================================================================================
-
- void GetUndoRedoText(Boolean cmdDone, CommandNumber aCommandNumber, CStr255& undoName);
- // Get the text for the undo or redo menu item for a command.
-
-
- #endif // __UUNDO__
-